package cn.b.sky.mes.hr.service.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import cn.b.sky.mes.cm.model.WorkCenter;
import cn.b.sky.mes.hr.dao.PersonnelDao;
import cn.b.sky.mes.hr.model.Personnel;
import cn.b.sky.mes.hr.model.condition.PersonnelCondition;
import cn.b.sky.mes.hr.service.PersonnelService;
import cn.b.sky.common.model.BatchJobReport;
import cn.b.sky.common.model.TreeInterface;
import cn.b.sky.common.utils.DatabaseUtils;
import cn.b.sky.common.utils.TreeUtil;
import cn.b.sky.i18n.utils.LocaleResourceBundle;
import cn.b.sky.user.SecurityContextUtil;

@Service
public class PersonnelServiceImpl implements PersonnelService {

  private static final Logger LOG = LoggerFactory.getLogger(PersonnelServiceImpl.class);

  @Autowired
  private PersonnelDao personnelDao;

  @Override
  public List<Personnel> getPersonnelListByCondition(PersonnelCondition condition) {
    return personnelDao.getPersonnelListByCondition(condition);
  }

  @Override
  public int getPersonnelCountByCondition(PersonnelCondition condition) {
    return personnelDao.getPersonnelCountByCondition(condition);
  }

  @Override
  public void savePersonnel(Personnel personnel) {
    if (personnel != null && !StringUtils.isBlank(personnel.getPersonnelCode())) {
      personnelDao.insertPersonnel(personnel);
    }
  }

  @Override
  public void updatePersonnel(Personnel personnel) {
    personnelDao.updatePersonnel(personnel);
  }

  @Override
  public Personnel getPersonnelInfo(String siteCode, String personnelCode) {
    return personnelDao.getPersonnelByPk(siteCode, personnelCode);
  }

  @Override
  public BatchJobReport deletePersonnel(String siteCode, String personnelCode) {
    String cause = "";
    Boolean success = true;
    try {
      Personnel personnel = new Personnel();
      personnel.setSiteCode(siteCode);
      personnel.setPersonnelCode(personnelCode);
      personnelDao.deletePersonnel(personnel);
    } catch (Exception e) {
      //    排班 等用到
      LOG.error("{}", e);
      TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
      if ((DatabaseUtils.isConstraintsError(e.getMessage()))) {
        // 外键删除已存在提示
        cause = LocaleResourceBundle.getCommonResourceLabel(SecurityContextUtil.getSiteCode(),SecurityContextUtil.getLanguageCode(),
            "dataBaseOperationPkFail.M")
            + LocaleResourceBundle.getCommonResourceLabel(SecurityContextUtil.getSiteCode(),SecurityContextUtil.getLanguageCode(), "notDelete.M");
      } else {
        cause = e.getMessage();
      }
      success = false;
    }
    return new BatchJobReport(personnelCode, cause, success, SecurityContextUtil.getLanguageCode(),SecurityContextUtil.getSiteCode());
  }

  @Override
  public List<WorkCenter> selectWorkCentersByShiftingPermission(String siteCode, String userCode) {
    return personnelDao.selectWorkCentersByShiftingPermission(siteCode, userCode, null);
  }

  @Override
  public List<TreeInterface> getWorkCenterTree(String siteCode, String userCode) {
    List<WorkCenter> workCenters = selectWorkCentersByShiftingPermission(siteCode, userCode);
    return TreeUtil.gradingRecursionSpecial(workCenters.toArray(new WorkCenter[workCenters.size()]), null);
  }

  @Override
  public List<Personnel> getPersonnelsByPermission(String siteCode, String userCode, String personnelCode,
      String workcenterCode) {
    return personnelDao.getPersonnelsByPermission(siteCode, userCode, personnelCode, workcenterCode);
  }

  @Override
  public List<Personnel> getPersonnelsSuggestByPermission(PersonnelCondition condition) {
    if (condition == null) {
      return null;
    }
    if (StringUtils.isBlank(condition.getOrderField())) {
      condition.setOrderField("createDateTime");//人员编号排序
    }

    return personnelDao.selectPersonnelsSuggestByPermission(condition);
  }

}
